package com.veterinaria.dao;

import java.io.Reader;
import java.util.ArrayList;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.veterinaria.bean.CitaBean;
import com.veterinaria.bean.PacienteBean;
import com.veterinaria.services.CitaService;

public class CitaDAO implements CitaService{
	
	SqlSessionFactory sqlmapper= null;{
	   	 try {
	   	 Reader reader = Resources.getResourceAsReader("ConfiguracionIbatis.xml");
	   	 sqlmapper =new  SqlSessionFactoryBuilder().build(reader);
	   	 } catch (Exception e) {
				e.printStackTrace();
			}
	    }
	
	@Override
	public void grabarCita(CitaBean obj) throws Exception {
		// TODO Auto-generated method stub
		SqlSession session = sqlmapper.openSession();
		String msg="";
		try {
			msg="exito"+ session.insert("citaxml.insertarCita",obj);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
			msg=e.getMessage();
			session.rollback();
		}finally{
			session.close();
		}
	}

	@Override
	public ArrayList<CitaBean> listarCita() throws Exception {
		// TODO Auto-generated method stub
		ArrayList<CitaBean> lista = null;
		SqlSession session = sqlmapper.openSession();
		try {
			lista = (ArrayList<CitaBean>)session.selectList("citaxml.listaCita");
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			session.close();
		}
		return lista;
	}

}
